// @ts-ignore
/* eslint-disable */
import { extend } from 'umi-request'

//创建一个新的request实例  类似于axios.create()
const Request = extend({
  timeout: 10000
})

//创建一个请求拦截，类似于axios.interceptors.request.use()
Request.interceptors.request.use((url, options) => {
  const tokenCode = localStorage.getItem('token')
  options.headers = { authorization: tokenCode as string }
  return { url, options }
})


//创建一个响应拦截 类似于axios.interceptors.response.use()
Request.interceptors.response.use(async (response, options) => {
  //解析响应结构体  返回内层数据
  const data = await response.clone().json();
  if (data.statusCode !== 200 || !data.success) {
    return Promise.reject(data)
  } else {
    return Promise.resolve(data.data)
  }
})


/** 获取当前的用户 GET /api/currentUser */
export async function currentUser(options?: { [key: string]: any }) {
  return Request<{
    data: API.CurrentUser;
  }>('/api/currentUser', {
    method: 'GET',
    ...(options || {}),
  });
}

/** 退出登录接口 POST /api/login/outLogin */
export async function outLogin(options?: { [key: string]: any }) {
  return Request<Record<string, any>>('/api/login/outLogin', {
    method: 'POST',
    ...(options || {}),
  });
}

/** 登录接口 POST/api/auth/login */
export async function login(body: API.LoginParams, options?: { [key: string]: any }) {
  return Request<API.LoginResult>('/api/auth/login', {
    method: 'POST',
    data: body,
    ...(options || {}),
  });
}

/** 此处后端没有提供注释 GET /api/notices */
export async function getNotices(options?: { [key: string]: any }) {
  return Request<API.NoticeIconList>('/api/notices', {
    method: 'GET',
    ...(options || {}),
  });
}

/** 获取规则列表 GET /api/rule */
export async function rule(
  params: {
    // query
    /** 当前的页码 */
    current?: number;
    /** 页面的容量 */
    pageSize?: number;
  },
  options?: { [key: string]: any },
) {
  return Request<API.RuleList>('/api/rule', {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  });
}

/** 新建规则 PUT /api/rule */
export async function updateRule(options?: { [key: string]: any }) {
  return Request<API.RuleListItem>('/api/rule', {
    method: 'PUT',
    ...(options || {}),
  });
}

/** 新建规则 POST /api/rule */
export async function addRule(options?: { [key: string]: any }) {
  return Request<API.RuleListItem>('/api/rule', {
    method: 'POST',
    ...(options || {}),
  });
}

/** 删除规则 DELETE /api/rule */
export async function removeRule(options?: { [key: string]: any }) {
  return Request<Record<string, any>>('/api/rule', {
    method: 'DELETE',
    ...(options || {}),
  });
}


//系统设置接口数据请求
export async function getSetting() {
  return Request('/api/setting/get', {
    method: 'POST'
  })
}
//工作台接口
export async function getListDashboard() {
  return Request('/api/article', {
    method: 'GET'
  })
}
export async function getListComments() {
  return Request('/api/comment', {
    method: 'GET'
  })
}
//工作台通过
export async function putListComments(id: string) {
  return Request(`/api/comment/${id}`, {
    data: { "pass": true },
    method: 'PATCH'
  })
}
//工作台拒绝
export async function patchListComments(id: string) {
  return Request(`/api/comment/${id}`, {
    data: { "pass": false },
    method: 'PATCH'
  })
}
//工作台回复
export async function postListComments(item: any) {
  return Request("/api/comment", {
    data: {
      content: item.content,
      email: item.email,
      hostId: item.hostId,
      name: item.name,
      parentCommentId: item.parentCommentId,
      replyUserEmail: item.replyUserEmail,
      replyUserName: item.replyUserName,
      url: item.url
    },
    method: 'POST'
  })
}
//工作台删除数据
export async function deleteListComments(params: string) {
  return Request(`/api/comment/${params}`, {
    method: 'DELETE'
  })
}
//页面管理
export async function getListPage() {
  return Request('/api/page', {
    method: 'GET'
  })
}
//页面管理搜索
export async function ListPage(value: any) {
  return Request('/api/page', {
    params: {
      status:value.status
    },
    method: 'GET'
  })
}
//页面管理状态
export async function patchListPage(item: any) {
  return Request(`/api/page/${item.id}`, {
    data: { status: item.status=="publish"?"draft":"publish" },
    method: 'PATCH'
  })
}
//页面管理删除
export async function deleteListPage(params: string) {
  return Request(`/api/page/${params}`, {
    method: 'DELETE'
  })
}
//知识小测
export async function getListKnowledge() {
  return Request('/api/knowledge', {
    method: 'GET'
  })
}
//知识小测搜索
export async function ListKnowledge(value: any) {
  return Request('/api/knowledge', {
    params: {
      ...value
    },
    method: 'GET'
  })
}
//知识小测删除
export async function deleteListKnowledge(id: string) {
  return Request(`/api/knowledge/${id}`, {
    method: 'DELETE'
  })
}
//知识小测状态
export async function patchListKnowledge(item: any) {
  return Request(`/api/knowledge/${item.id}`, {
    data: { status: item.status=="publish"?"draft":"publish" },
    method: 'PATCH'
  })
}


//用户管理信息
export async function getUser(params?:{[key:string]:any}){
  return Request('/api/user/update', {
    method: 'POST',
    ...(params||{})
  })
}

//用户管理数据  搜索
export async function getUserList(params?:{[key:string]:any}){
  let name='';
  let email='';
  let role='';
  let status='';
  if(params!=undefined){
    if(params.name){
       name=params.name;
    }else{
      name='';
    }
    
    
    if(params.email){
       email=params.email;
    }else{
      email='';
    } 
    
    if(params.role){
      role=params.role;
    }else{
      role='';
    } 

    if(params.status){
      status=params.status;
    }else{
      status='';
    } 
  }

  return Request(`/api/user?name=${name}&email=${email}&role=${role}&status=${status}`, {
    method: 'GET',
    ...(params||{})
  })
}

